package meterMonitor.framework.auth;
 
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 

import meterMonitor.util.ResourceConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
 
  
public class AuthorityAnnotationInterceptor extends HandlerInterceptorAdapter {
	 final  Logger logger = LoggerFactory.getLogger(getClass());
 

		@Autowired
		protected ResourceConfig resourceConfig;
		
	    @Override
	    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//	    	 String path = request.getRequestURI();   
				
            HandlerMethod handler2=(HandlerMethod) handler;
	        FireAuthority fireAuthority = handler2.getMethodAnnotation(FireAuthority.class);
	 
	        if(null == fireAuthority){
	            //没有声明权限,放行
	            return true;
	        }
	        
//	        if(SessionContainer.getOperatorId() == null) {
//	            //throw new NoPermissionException("你还没有登陆！");//&redirectUrl=当前请求的url
//	            request.getRequestDispatcher(" http://passport.zhaogangrentest.com/login?zg_sso_system_alias=interest.zhengxin.management.ui").forward(request, response);
//	        }
	        boolean aflag;
	       
	        String authCode=fireAuthority.authorityTypes()[0].getIndex();
	       
//	    	RequestUserFunctionSystemModel authRequest=new RequestUserFunctionSystemModel(); 
//	    	authRequest.setUserId(SessionContainer.getOperatorId().toString()); 
//	    	String systemCode=resourceConfig.getValueByKey("systemCode");
//	    	List<String> systemCodeList=new ArrayList<String>();
//	    	systemCodeList.add(systemCode); 
//	    	authRequest.setSystemCode(systemCodeList);//系统别名
//	    	authRequest.setFunctionCode(authCode);//Code标识
//	        ResponseHaveFunctionModel authResponse = functionService.checkAcfunctionByOperator(authRequest);
	      //  if (authResponse!=null&&!authResponse.getHaveFunction()) {
	        if(false){
	           //没有权限！ 
	        	aflag=false;
	        }
	        else
	        {
	            //有权限
	        	aflag=true;
	        } 
	 
	        if(false == aflag){//没有权限
	 
	            if (fireAuthority.resultType() == ResultTypeEnum.page) {
	                 //跳转页面              
                	 request.getRequestDispatcher("/authFail.do").forward(request, response);
	            } else if (fireAuthority.resultType() == ResultTypeEnum.json) {
	                //ajax类型的登录提示
	                response.setCharacterEncoding("utf-8");
	               // response.setContentType("application/json;charset=UTF-8");
	                response.setHeader("Content-Type", "application/json;charset=UTF-8"); 
	                OutputStream out = response.getOutputStream();
	            	//String strJsob = IOUtils.toString(out,CommonConsts.ENCODING);
	        		
	                PrintWriter pw = new PrintWriter(new OutputStreamWriter(out,"utf-8"));
	                pw.println("{\"code\":-1,\"success\":false,\"msg\":\"权限不足！\"}");
	                pw.flush();
	                pw.close();
	            }
	 
	            return false;
	 
	        }
	        return true;
	    }
}
